<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 广告
 */
namespace app\home\model;

use app\home\model\Model;

class Advert extends Model{

	private $adtable = 'web_advert';
    private $adclasstable = 'web_advertclass';

    /**
     * 广告列表
     * @param array $userlist
     * @param array $where
     */
    public function advertList($where='',$lang='default',$page=1,$page_size=10)
    {
        $advertlist = Model::name($this->adtable)
            ->field('*')
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('sortid desc')
            ->select()
            ->toArray();
        foreach ($advertlist as $key => $value) {
            if(!empty($value['introduction'])){
                $advertlist[$key]['introduction']=isset(json_decode($value['introduction'],true)[$lang])?json_decode($value['introduction'],true)[$lang]:'';  
            }
            if(!empty($value['chaintitle'])){
                $advertlist[$key]['chaintitle']=isset(json_decode($value['chaintitle'],true)[$lang])?json_decode($value['chaintitle'],true)[$lang]:'';
            }
            if(!empty($value['titles'])){
                $advertlist[$key]['title']=isset(json_decode($value['titles'],true)[$lang])?json_decode($value['titles'],true)[$lang]:$value['title'];
            }
            unset($advertlist[$key]['titles']);
        }
        return $advertlist;
    }

    /**
     * 广告列表
     * @param array $advert
     * @param array $where
     */
    public function advert($where='',$lang='default')
    {
        $advert = Model::name($this->adtable)
            ->field('*')
            ->where($where)
            ->limit(0,1)
            ->order('sortid desc')
            ->find();
        if(!empty($advert['introduction'])){
            $advert['introduction']=isset(json_decode($advert['introduction'],true)[$lang])?json_decode($advert['introduction'],true)[$lang]:'';  
        }
        if(!empty($advert['chaintitle'])){
            $advert['chaintitle']=isset(json_decode($advert['chaintitle'],true)[$lang])?json_decode($advert['chaintitle'],true)[$lang]:'';
        }
        if(!empty($advert['titles'])){
            $advert['title']=isset(json_decode($advert['titles'],true)[$lang])?json_decode($advert['titles'],true)[$lang]:$advert['title'];
        }
        unset($advert['titles']);
        return $advert;
    }

    /**
     * 广告类别列表
     * @param array $advertclasslist
     * @param array $where
     */
    public function advertClassList($where='',$page=1,$page_size=10)
    {
        $advertclasslist = Model::name($this->adclasstable)
            ->field('*')
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('sortid desc')
            ->select()
            ->toArray();
        return $advertclasslist;
    }

    /**
     * 获取用户
     * @param $value
    */
    public function getAdClass($where='',$file='*')
    {
        $res = Model::name($this->adclasstable)->field($file)->where($where)->find();
        return $res;
    }

}
